//index.js
//获取应用实例
var user  = require('../../utils/user.js')
var app = getApp()
Page({
  data: {
    lg_bc_lf:'lg-bc',
    lg_bc_rg:'',
    loginInfo:[],
    login: false,
    dialog: {
      title: '',
      content: '',
      hidden: true
    }
  },
  change_bc_lf:function(){
    this.setData({
      lg_bc_lf:'lg-bc',
      lg_bc_rg:''
    })
  },
  change_bc_rg:function(){
    this.setData({
      lg_bc_rg:'lg-bc',
      lg_bc_lf:''
    })
  },
  login:function(data){
    var that = this
    wx.showNavigationBarLoading()
    
    var data = {'pass':data.detail.value.pass,'mobile':data.detail.value.mobile}
    user.login(data,function(res){
      wx.hideNavigationBarLoading()
      wx.setStorageSync('$token', res.data.data.token)
      that.setData({login:true})
      wx.showToast({
        title: '登录成功',
        icon: 'success',
        duration: 2000
      })
      if ("undefined" != typeof that.data.url) {
          wx.redirectTo({
          url: that.data.url
        })
      }else{
        wx.switchTab({
          url: '/pages/index/index',
          success: function(res){
            // success
          },
          fail: function(res) {
            // fail
          },
          complete: function(res) {
            // complete
          }
        })
      }
      
    })
    wx.hideNavigationBarLoading()
  },
  logout: function() {
    var that = this
    wx.showNavigationBarLoading()
    user.logout()
    that.setData({login:false})
    wx.hideNavigationBarLoading()
  },
  onLoad:function(options){

    if ("undefined" != typeof options.url) {
      var url = options.url + '?'
      for(var key in options){
         url += '&' + key + '=' + options[key]
      }
      console.log(options)
      this.setData({
        url:url
      })
    }
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    if (user.checkLogin()) {
      wx.switchTab({
        url: '/pages/index/index',
        success: function(res){
          // success
        },
        fail: function(res) {
          // fail
        },
        complete: function(res) {
          // complete
        }
      })
      this.setData({login:true})
    }else{
      this.setData({login:false})
    }
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
    
})
